package com.guowen.myaccount.frag_sort;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.guowen.myaccount.R;
import com.guowen.myaccount.db.TypeBean;
import com.guowen.myaccount.frag_record.TypeBaseAdapter;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseSortFragment extends Fragment implements View.OnClickListener{

    List<TypeBean> typeList;
    TypeBaseAdapter adapter;

    private GridView sortGv;
    private Button addBtn;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sort,container,false);
        initView(view);
        // 给GridView填充数据的方法
        loadDataGrid();
        // 设置GridView每一项的点击事件
        setGVListener();
        return view;

    }

    private void setGVListener() {
        sortGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.selectPos = position;
                adapter.notifyDataSetChanged(); //提示绘制发生变化
                TypeBean typeBean = typeList.get(position);
                Intent intent = new Intent(getContext(), ShowTypeActivity.class);
                intent.putExtra("bean",typeBean);
                getContext().startActivity(intent);
            }
        });
    }

    public void loadDataGrid() {
        typeList = new ArrayList<>();
        adapter = new TypeBaseAdapter(getContext(), typeList);
        sortGv.setAdapter(adapter);
    }

    private void initView(View view) {
        sortGv = view.findViewById(R.id.frag_sort_gv);
        addBtn = view.findViewById(R.id.frag_sort_btn);

        addBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.frag_sort_btn:
                break;
        }
    }
}
